package com.fh.wanandroid.ui.knowledge.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.fh.wanandroid.R;
import com.fh.wanandroid.base.BaseActivity;
import com.fh.wanandroid.base.BasePresenter;
import com.fh.wanandroid.ui.knowledge.KnowledgeDataList;
import com.fh.wanandroid.ui.knowledge.fragment.KnowledgeListFragment;
import com.fh.wanandroid.utils.StatusBarUtil;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KnowledgeDetailActivity extends BaseActivity {


    @BindView(R.id.common_toolbar_title_tv)
    TextView commonToolbarTitleTv;
    @BindView(R.id.common_toolbar)
    Toolbar commonToolbar;
    @BindView(R.id.know_detail_viewpager)
    ViewPager knowDetailViewpager;
    @BindView(R.id.knowledge_detail_tab_layout)
    SlidingTabLayout knowledgeDetailTabLayout;

    List<KnowledgeListFragment> mFragments;


    List<KnowledgeDataList> knowledgeDataList;


    @Override
    public BasePresenter getmPresenter() {
        return null;
    }

    @Override
    public void presenterSetView() {

    }

    @Override
    public void initView() {

        //设置toolbar
        setSupportActionBar(commonToolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayShowTitleEnabled(false);
        StatusBarUtil.setStatusColor(getWindow(), ContextCompat.getColor(this, R.color.main_status_bar_blue), 1f);
//        IndexVpAdapter indexVpAdapter = new IndexVpAdapter(getChildFragmentManager(), this.getContext());
//        knowDetailViewpager.setAdapter(indexVpAdapter);

    }

    @Override
    public void initData() {

        KnowledgeDataList knowledgeData = (KnowledgeDataList) getIntent().getSerializableExtra("knowledgeDataList");


        if (knowledgeData == null || knowledgeData.getName() == null) {
            return;
        }

        Log.e("knowdetails", "-------------------knowledge name:" + knowledgeData.getName());

        commonToolbarTitleTv.setText(knowledgeData.getName());

        mFragments = new ArrayList<>();


        knowledgeDataList = knowledgeData.getChildren();

        if (knowledgeDataList == null) {
            return;
        }


        for (KnowledgeDataList data : knowledgeDataList) {
            mFragments.add(KnowledgeListFragment.getInstance(data.getName(), data.getId()));
        }

        KnowledgeVpAdaper knowledgeVpAdaper = new KnowledgeVpAdaper(getSupportFragmentManager(), this, mFragments);
        knowDetailViewpager.setAdapter(knowledgeVpAdaper);
        knowledgeDetailTabLayout.setViewPager(knowDetailViewpager);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_knowledge_detail;
    }



    public class KnowledgeVpAdaper extends FragmentPagerAdapter {

        List<KnowledgeListFragment> fragmentList;

        Context context;

        public KnowledgeVpAdaper(FragmentManager fm, Context context, List<KnowledgeListFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;

        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return knowledgeDataList.get(position).getName();
        }
    }

}
